package com.wjh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjh.enums.AppHttpCodeEnum;
import com.wjh.exception.SystemException;
import com.wjh.mapper.ExpressageMapper;
import com.wjh.pojo.Expressage;
import com.wjh.pojo.FlowerType;
import com.wjh.pojo.LogisticsStaff;
import com.wjh.service.ExpressageService;
import com.wjh.service.LogisticsStaffService;
import com.wjh.utils.ResponseResult;
import com.wjh.vo.PageVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/**
 * (Expressage)表服务实现类
 *
 * @author makejava
 * @since 2024-03-10 21:35:42
 */
@Service("expressageService")
public class ExpressageServiceImpl extends ServiceImpl<ExpressageMapper, Expressage> implements ExpressageService {

    @Resource
    private LogisticsStaffService logisticsStaffService;



    @Override
    public ResponseResult getExpressageAll(Integer pageNum, Integer pageSize, String phone, String orderNumber, String fetchGoodsNumber,Integer state) {
        LambdaQueryWrapper<Expressage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(phone!=null,Expressage::getPhone,phone);
        queryWrapper.like(orderNumber!=null,Expressage::getOrderNumber,orderNumber);
        queryWrapper.like(fetchGoodsNumber!=null,Expressage::getFetchGoodsNumber,fetchGoodsNumber);
        queryWrapper.eq(state != null,Expressage::getState,state);
        //分页查询所有
        Page<Expressage> expressagePage = new Page<>(pageNum,pageSize);
        Page<Expressage> page = page(expressagePage, queryWrapper);
        List<Expressage> records = page.getRecords();
        for (Expressage record : records) {
            record.setCourierName(logisticsStaffService.getStaffById(record.getCourierId()).getName());
        }
        return ResponseResult.okResult(new PageVo(records,page.getTotal()));
    }

    @Override
    public ResponseResult updateExpressage(Expressage expressage) {
        if(!StringUtils.hasText(expressage.getAddress())){
            throw new SystemException(AppHttpCodeEnum.STAFF_ADDRESS_NOTNULL);
        }
        if(!StringUtils.hasText(expressage.getPhone())){
            throw new SystemException(AppHttpCodeEnum.STAFF_PHONE_ERROR);
        }
        if(!StringUtils.hasText(expressage.getCourierName())){
            throw new SystemException(AppHttpCodeEnum.STAFF_STAFF_NAME_NOTNULL);
        }
        if(!StringUtils.hasText(expressage.getFetchGoodsPerson())){
            throw new SystemException(AppHttpCodeEnum.STAFF_USER_PHONE_NOTNULL);
        }
        if(!StringUtils.hasText(expressage.getOrderNumber())){
            throw new SystemException(AppHttpCodeEnum.STAFF_ORDER_NUMBER_NOTNULL);
        }
        if(!StringUtils.hasText(expressage.getFetchGoodsNumber())){
            throw new SystemException(AppHttpCodeEnum.STAFF_FETCH_GOODS_NUMBER_NOTNULL);
        }
        if(expressage.getState() == null){
            throw new SystemException(AppHttpCodeEnum.STAFF_STAFF_STATE_NOTNULL);
        }
        if(expressage.getCourierId() == null){
            throw new SystemException(AppHttpCodeEnum.STAFF_STAFF_NAME_NOTNULL);
        }
        if(expressage.getId() == null){
            throw new SystemException(AppHttpCodeEnum.ID_NOTNULL);
        }
        updateById(expressage);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult addExpressage(Expressage expressage) {
        if(!StringUtils.hasText(expressage.getAddress())){
            throw new SystemException(AppHttpCodeEnum.STAFF_ADDRESS_NOTNULL);
        }
        if(!StringUtils.hasText(expressage.getPredictTime())){
            throw new SystemException(AppHttpCodeEnum.STAFF_PREDICT_TIME_NOTNULL);
        }
        if(!StringUtils.hasText(expressage.getPhone())){
            throw new SystemException(AppHttpCodeEnum.STAFF_PHONE_ERROR);
        }
        if(!StringUtils.hasText(expressage.getFetchGoodsPerson())){
            throw new SystemException(AppHttpCodeEnum.STAFF_USER_PHONE_NOTNULL);
        }
        if(!StringUtils.hasText(expressage.getOrderNumber())){
            throw new SystemException(AppHttpCodeEnum.STAFF_ORDER_NUMBER_NOTNULL);
        }
        if(!StringUtils.hasText(expressage.getFetchGoodsNumber())){
            throw new SystemException(AppHttpCodeEnum.STAFF_FETCH_GOODS_NUMBER_NOTNULL);
        }
        if(expressage.getState() == null){
            throw new SystemException(AppHttpCodeEnum.STAFF_STAFF_STATE_NOTNULL);
        }
        if(expressage.getCourierId() == null){
            throw new SystemException(AppHttpCodeEnum.STAFF_STAFF_NAME_NOTNULL);
        }
        //添加包裹创建日期数据
        LocalDate now = LocalDate.now();
        expressage.setCreateTime(now.getYear()+"-"+now.getMonthValue()+"-"+now.getDayOfMonth());
        save(expressage);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delExpressageByIds(List<Integer> id) {
        removeByIds(id);
        return ResponseResult.okResult();
    }
}
